package com.ibeeking.found.common.exception;

import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.exception.DataNotExistException;
import com.ibeeking.nematos.exception.OperationException;
import com.ibeeking.nematos.exception.SystemException;
import com.ibeeking.nematos.utils.data.StringUtils;
import com.ibeeking.nematos.utils.result.ResponseCodeEnum;
import com.ibeeking.nematos.utils.result.ResponseResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.io.EOFException;
import java.io.FileNotFoundException;

/**
 * @ClassName ExceptionControllerAdvice
 * @Description 全局异常处理
 * @Author ibeeking
 * @Date 2020-10-13 17:38
 **/
@RestControllerAdvice
public class ExceptionControllerAdvice {
    private static final Logger log = LoggerFactory.getLogger(ExceptionControllerAdvice.class);

    @ExceptionHandler(Exception.class)
    public ResponseResult<String> ExceptionHandler(Exception e) {
        if (e instanceof BusinessException) {
            //手动抛出的业务异常,以warn级别打印
            log.warn(e.getMessage(), e);
        } else {
            log.error(e.getMessage(), e);
        }
        if (e instanceof BusinessException) {
            return ResponseResult.fail(((BusinessException) e).getCode(), e.getMessage());
        } else if (e instanceof DataNotExistException) {
            return ResponseResult.fail(((DataNotExistException) e).getCode(), e.getMessage());
        } else if (e instanceof OperationException) {
            return ResponseResult.fail(((OperationException) e).getCode(), e.getMessage());
        } else if (e instanceof SystemException) {
            return ResponseResult.fail(((SystemException) e).getCode(), e.getMessage());
        } else if (e instanceof MethodArgumentNotValidException) {
            //方法参数校验异常
            return ResponseResult.fail(ResponseCodeEnum.PARAM_CHECK_FAIL.getCode(), ((MethodArgumentNotValidException) e).getBindingResult().getAllErrors().get(0).getDefaultMessage());
        } else if (e instanceof NullPointerException) {
            //空指针异常
            return ResponseResult.fail(ResponseCodeEnum.SYSTEM_ERROR.getCode(), "空指针异常");
        } else if (e instanceof ClassNotFoundException) {
            //类找不到异常
            return ResponseResult.fail(ResponseCodeEnum.SYSTEM_ERROR.getCode(), e.getMessage());
        } else if (e instanceof IndexOutOfBoundsException) {
            //下标越界异常
            return ResponseResult.fail(ResponseCodeEnum.SYSTEM_ERROR.getCode(), e.getMessage());
        } else if (e instanceof IllegalArgumentException) {
            //方法参数错误异常
            return ResponseResult.fail(ResponseCodeEnum.SYSTEM_ERROR.getCode(), e.getMessage());
        } else if (e instanceof IllegalAccessException) {
            //没有访问权限异常
            return ResponseResult.fail(ResponseCodeEnum.SYSTEM_ERROR.getCode(), e.getMessage());
        } else if (e instanceof ArithmeticException) {
            //数学运算异常
            return ResponseResult.fail(ResponseCodeEnum.SYSTEM_ERROR.getCode(), e.getMessage());
        } else if (e instanceof ClassCastException) {
            //数据类型转换异常
            return ResponseResult.fail(ResponseCodeEnum.SYSTEM_ERROR.getCode(), e.getMessage());
        } else if (e instanceof NoSuchMethodException) {
            //方法不存在异常
            return ResponseResult.fail(ResponseCodeEnum.SYSTEM_ERROR.getCode(), e.getMessage());
        } else if (e instanceof FileNotFoundException) {
            //文件找不到异常
            return ResponseResult.fail(ResponseCodeEnum.SYSTEM_ERROR.getCode(), e.getMessage());
        } else if (e instanceof EOFException) {
            //文件已结束异常
            return ResponseResult.fail(ResponseCodeEnum.SYSTEM_ERROR.getCode(), e.getMessage());
        } else if (e instanceof InstantiationException) {
            //实例化异常
            return ResponseResult.fail(ResponseCodeEnum.SYSTEM_ERROR.getCode(), e.getMessage());
        } else if (e instanceof InterruptedException) {
            //被终止异常
            return ResponseResult.fail(ResponseCodeEnum.SYSTEM_ERROR.getCode(), e.getMessage());
        } else if (e instanceof CloneNotSupportedException) {
            //不支持克隆异常
            return ResponseResult.fail(ResponseCodeEnum.SYSTEM_ERROR.getCode(), e.getMessage());
        } else {
            return ResponseResult.fail(ResponseCodeEnum.UNKNOWN_ERROR.getCode(), StringUtils.isBlank(e.getMessage()) ? ResponseCodeEnum.UNKNOWN_ERROR.getMsg() : e.getMessage());
        }
    }
}
